package b1.scenes.tower
{
	import as3isolib.core.*;
	import as3isolib.display.*;
	import as3isolib.display.primitive.*;
	import as3isolib.display.renderers.*;
	import as3isolib.display.scene.*;
	import as3isolib.geom.*;
	
	import b1.conifg.story.*;
	import b1.net.*;
	import b1.players.*;
	import b1.utils.*;
	
	import com.greensock.*;
	
	import eDpLib.events.ProxyEvent;
	
	import flash.display.*;
	import flash.events.*;

	public class TowerLayer extends Sprite
	{
		public var isoScene:IsoScene;
		public var isoView:IsoView;
		private var myAnimalGrid:AnimalGrid;
		private var towerLayerConfig:TowerLayerConfig;
		//所有的魔塔元素(以字符串"posX_posY"为索引）
		public var towerGrids:Object;
		//所有的动物元素(以字符串"animal id"为索引）
		public var animalGrids:Object;
		public static const gridWidth:int = 25;
		public static const hoverHeight:Number = 0;
		
		public function TowerLayer(towerLayerConfig:TowerLayerConfig)
		{
			super();
			towerGrids = new Object();
			animalGrids = new Object();
			this.towerLayerConfig = towerLayerConfig;
			var gridNumX:int = towerLayerConfig.mapRows.length;
			var gridNumY:int = towerLayerConfig.mapRows[0].length;
			var LayerBgr:Class = ResourceHelper.getInstance().getClassByName("WestSuburb");
			addChild(new LayerBgr());
			//场景               
			isoScene = new IsoScene();                        
			isoScene.hostContainer = this;
			//坐标
			var g:IsoGrid = new IsoGrid();
			g.setGridSize(gridNumX, gridNumY, 2);
			g.showOrigin = false;
			g.container.alpha = 1;
			g.addEventListener(MouseEvent.CLICK, grid_mouseHandler);
			isoScene.addChild(g);
			//元素
			for (var i:int = 0; i < gridNumX; i++)
			{
				var mapRow:Array = towerLayerConfig.mapRows[i];
				for (var j:int = 0; j < gridNumY; j++)
				{
					var mapGrid:TowerGridConfig = mapRow[j];
					var GridClass:Class;
					if (mapGrid.texture > 0)
					{
						var textureGrid:TowerGrid = new TowerGrid();
						textureGrid.setSize(gridWidth, gridWidth, 1);
						textureGrid.moveBy(i * gridWidth, j * gridWidth, -1);                        
						isoScene.addChild(textureGrid);
	                    GridClass = ResourceHelper.getInstance().getClassByName("TextureGrid" + mapGrid.texture);
	                    textureGrid.sprites = [GridClass];
	                    textureGrid.container.mouseEnabled = false;
	                    textureGrid.container.mouseChildren = false;
					}
					if (mapGrid.animalId < 0)
					{
						var animalGrid:AnimalGrid = new AnimalGrid(mapGrid.animalId);
						animalGrid.setSize(gridWidth, gridWidth, gridWidth);
						animalGrid.moveBy(mapGrid.posX * gridWidth, mapGrid.posY * gridWidth, hoverHeight);
						animalGrids[mapGrid.animalId] = animalGrid;                        
						isoScene.addChild(animalGrid);
					}
					else if (mapGrid.type == 2)
					{
						var me:PlayerInfo = PlayerInfoHelper.getInstance().players[PlayerInfoHelper.getInstance().userId];
						myAnimalGrid = new AnimalGrid(me.ownAnimals[0]);
						myAnimalGrid.setSize(gridWidth, gridWidth, gridWidth);
						myAnimalGrid.moveBy(mapGrid.posX * gridWidth, mapGrid.posY * gridWidth, hoverHeight);
						animalGrids[me.ownAnimals[0]] = myAnimalGrid;                        
						isoScene.addChild(myAnimalGrid);
					}
					else if (mapGrid.type > 0)
					{
						var towerGrid:TowerGrid = new TowerGrid(mapGrid.type);
						towerGrid.setSize(gridWidth, gridWidth, gridWidth);
						towerGrid.moveBy(i * gridWidth, j * gridWidth, hoverHeight);
						towerGrids[i + "_" + j] = towerGrid;                        
						isoScene.addChild(towerGrid);
	    			}
				}
			}
			//阴影
			var factory:as3isolib.core.ClassFactory = new as3isolib.core.ClassFactory(DefaultShadowRenderer);
			factory.properties = {shadowColor:0x000000, shadowAlpha:0.15, drawAll:false};                        
			isoScene.styleRenderers = [factory];                                                
			isoScene.render();
			//设置视角                                
			isoView = new IsoView();                        
			isoView.setSize(BFont.stageWidth, BFont.stageHeight);
			isoView.zoom(1 * 12 / gridNumX);                        
			isoView.centerOnPt(new Pt(gridNumX * gridWidth / 2, gridNumY * gridWidth / 2, 0));
			isoView.addScene(isoScene);
			addChild(isoView);
		}
		
		public function removeTowerGrid(posX:int, posY:int):void
		{
			if (towerGrids[posX + "_" + posY] != null)
			{
				isoScene.removeChild(towerGrids[posX + "_" + posY]);
				delete towerGrids[posX + "_" + posY];
			}
		}
		
		private function grid_mouseHandler(evt:ProxyEvent):void
		{
			var mEvt:MouseEvent = MouseEvent(evt.targetEvent);        
			var pt:Pt = new Pt(mEvt.localX, mEvt.localY);        
			IsoMath.screenToIso(pt);
			
			myAnimalGrid.path = towerLayerConfig.findPath(myAnimalGrid.x / gridWidth, myAnimalGrid.y / gridWidth, pt.x / gridWidth, pt.y / gridWidth);
			myAnimalGrid.startMove();
			if (!hasEventListener(Event.ENTER_FRAME))                
				this.addEventListener(Event.ENTER_FRAME, enterFrameHandler);
		}

		private function completeCallback():void
		{        
			this.removeEventListener(Event.ENTER_FRAME, enterFrameHandler);
		}
				
		private function enterFrameHandler(evt:Event):void
		{        
			isoScene.render();
		}
		
		public function destroy():void
		{
			
		}
				
	}
}